package apogeu.monitora.models.negocio;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

import apogeu.monitora.models.Sessao;
import apogeu.monitora.models.dados.Formulario2;
import apogeu.monitora.models.dados.IPersistenciaFormulario;

public class NegocioFormulario2 implements IPersistenciaFormulario<Formulario2>, Serializable {

	private static final long serialVersionUID = 1L;

	@Override
	public boolean inserir(Formulario2 objeto) {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			
			//if (this.remover(objeto.getPrefeitura().getCdPrefeitura())) {
				session.save(objeto);
			//}
			
			transaction.commit();
			
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		
		return false;
	}

	@Override
	public boolean remover(Formulario2 objeto) {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			
			session.delete(objeto);
			
			transaction.commit();
			
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		
		return false;
	}

	@Override
	public boolean remover(int id) {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			
			session.createQuery("DELETE FROM Formulario2 form " +
					"WHERE form.prefeitura.cdPrefeitura = :codigo")
					.setInteger("codigo", id)
					.executeUpdate();
			
			//session.delete(formulario1);
			
			transaction.commit();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		
		return false;
	}

	@Override
	public boolean alterar(Formulario2 objeto) {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			
			session.update(objeto);
			
			transaction.commit();
			
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		
		return false;
	}

	@Override
	public Formulario2 buscar(int id) {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			
			Formulario2 formulario2 = (Formulario2) session.getNamedQuery("formulario2_codigo")
										.setInteger("CODIGO", id)
										.uniqueResult();
			
			return formulario2;
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		
		return null;
	}

	@Override
	public Formulario2 buscarSemArquivo(int id) {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			
			Formulario2 formulario2 = (Formulario2) session.getNamedQuery("formulario2_codigo_sem_arquivo")
										.setParameter("CODIGO", id)
										.uniqueResult();
			
			return formulario2;
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		
		return null;
	}

	@Override
	public List<Formulario2> buscarTodos() {
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			
			List<Formulario2> lista = session.getNamedQuery("formulario2").list();
			
			return lista;
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		
		return null;
	}
	
	@Override
	public Formulario2 buscarNomeArquivo(int codigo_prefeitura, int arquivo) {
		String nomeArquivo = "";
		String strArquivo = "";
		
		switch (arquivo) {
			case 1:
				nomeArquivo = "qa2n";
				strArquivo = "q2a";
				break;
			case 2:
				nomeArquivo = "qa10n";
				strArquivo = "q10a";
				break;
			case 3:
				nomeArquivo = "qa11sn";
				strArquivo = "q11sa";
				break;
			case 4:
				nomeArquivo = "qa13n";
				strArquivo = "q13a";
				break;
		}
		
		SessionFactory sessionFactory = null;
		Session session = null;
		
		try {
			sessionFactory = Sessao.getSessionFactory();
			session = sessionFactory.openSession();
			
			Criteria criteria = session.createCriteria(Formulario2.class);
			criteria.setFetchMode("prefeitura", FetchMode.EAGER);
			criteria.add(Restrictions.eq("prefeitura.cdPrefeitura", codigo_prefeitura));
			criteria.setProjection(Projections.projectionList()
							.add(Projections.property(nomeArquivo).as(nomeArquivo))
							.add(Projections.property(strArquivo).as(strArquivo))
							);
			criteria.setResultTransformer(new AliasToBeanResultTransformer(Formulario2.class));
			
			Formulario2 formulario2 = (Formulario2) criteria.uniqueResult();
			
			return formulario2;
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) session.close();
			if (sessionFactory != null) sessionFactory.close();
		}
		
		return null;
	}
}
